# -*- coding:utf-8 -*-
from __future__ import unicode_literals

import re

from django.contrib import admin

from wlapps.utils.common_a import manual_save
from wlmodels.codes.utils import ELBaseAdmin, EntIdableAdmin
from .models import *


class ELBasicInline(admin.StackedInline):
    model = ELBasic
    can_delete = False
    extra = 0


class ELInvInline(admin.StackedInline):
    model = ELInv
    can_delete = False
    extra = 0
    raw_id_fields = ("personid",)


class ELManagerInline(admin.StackedInline):
    model = ELManager
    can_delete = False
    extra = 0
    raw_id_fields = ("personid",)


class ELPostAdmin(ELBaseAdmin):
    # list_display = ["entid", "ENTNAME", "REGNO", "NACAOID", "updated", "admin_link"]
    list_display = ["entid", "ENTNAME", "REGNO", "NACAOID", "updated"]

    list_display_links = ["ENTNAME", ]
    search_fields = ("^ENTNAME",)
    readonly_fields = ("entid", "updated")
    actions = [manual_save, ]
    inlines = [ELBasicInline, ELManagerInline, ELInvInline, ]


class ELBasicAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "ESDATE", "ENTTYPE", "ENTSTATUS", "REGCAP", "EMPNUM"]
    list_display_links = ["entid", ]
    list_filter = ["ENTSTATUS", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)


class ELClassAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "region_id", "nic_id", "el_seic_id", "lat", "lng", ]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)


class ELShortAdmin(ELBaseAdmin):
    list_display = ["id", "ENTNAME", "shortname", "NAMEIND", "updated", ]
    list_display_links = ["ENTNAME", ]
    search_fields = ("^ENTNAME",)
    readonly_fields = ("updated",)


class ELNameHistoryAdmin(EntIdableAdmin):
    list_display = ["id", "entid_id", "ENTNAME", ]
    list_display_links = ["entid_id", ]


class ELTagsAdmin(ELBaseAdmin):
    list_display = ["id", "name", "count", ]
    list_display_links = ["name", ]
    search_fields = ("name",)
    readonly_fields = ("count",)


class ELTagsAssignedAdmin(EntIdableAdmin):
    list_display = ["id", "tag_id", "entid", "created", "updated", ]
    raw_id_fields = ("tag_id",)
    readonly_fields = ("updated",)


class ELManagerAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "NAME", "POSITION", "NATDATE", "LEREPSIGN", ]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj", "personid",)
    readonly_fields = ("updated",)

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(ELManagerAdmin, self).get_search_results(request, queryset, search_term)
        if search_term:
            zhpattern = re.compile(u'[\u4e00-\u9fa5]+')
            match = zhpattern.search(search_term)
            if match:
                from wlmodels.company.models import ELPost
                the_coms = ELPost.objects.filter(ENTNAME__istartswith=search_term).values_list("entid", flat=True)[:300]
                queryset = self.model.objects.filter(entobj__in=list(the_coms))
            else:
                queryset = self.model.objects.filter(CERNO__istartswith=search_term)
        return queryset, use_distinct


class ELModifyAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "ALTITEM", "ALTDATE", "ALTTIME", "updated", ]
    list_display_links = ["entid", ]
    list_filter = ["ALTITEM", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)
    readonly_fields = ("updated",)


#####################
###### 个体工商户 ######
#####################

class ELGetiPersonInline(admin.StackedInline):
    model = ELGetiPerson
    can_delete = False
    extra = 0
    raw_id_fields = ("personid",)


class ELGetiModifyInline(admin.StackedInline):
    model = ELGetiModify
    can_delete = False
    extra = 0


class ELGetiRevokeInline(admin.StackedInline):
    model = ELGetiRevoke
    can_delete = False
    extra = 0


class ELGetiCancelInline(admin.StackedInline):
    model = ELGetiCancel
    can_delete = False
    extra = 0


class ELGetiAdmin(ELBaseAdmin):
    list_display = ["id", "ENTNAME", "faren", "REGNO", "REGCAP", "ENTSTATUS", "ESDATE", "updated", ]
    list_display_links = ["ENTNAME", ]
    list_filter = ["ENTSTATUS", ]
    search_fields = ("^ENTNAME",)
    readonly_fields = ("entid", "updated")
    actions = [manual_save, ]
    inlines = [ELGetiPersonInline, ELGetiModifyInline, ELGetiCancelInline, ELGetiRevokeInline]


#####################

class ELLiqentidationAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "LIGENTITY", "LIGST", "LIGENDDATE", "updated"]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)
    readonly_fields = ["updated", ]


class ELCaseBasicAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "caseid", "NAME", "LEREP", "PARTYTYPE", "updated"]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj", "personid",)
    readonly_fields = ("updated",)


class ELCaseDetailAdmin(ELBaseAdmin):
    list_display = ["id", "caseid", "CASENAME", "CASESPOT", "CASESCEDISTRICT", "updated"]
    list_display_links = ["CASENAME", ]
    search_fields = ("caseid",)
    readonly_fields = ["updated", ]


class ELCasePunishAdmin(ELBaseAdmin):
    list_display = ["id", "caseid", "ILLEGACTTYPE", "PENAM", "FORFAM", "updated"]
    list_display_links = ["caseid", ]
    search_fields = ("caseid",)
    readonly_fields = ["updated", ]


class ELCancelAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "CANREA", "CANDATE", "PUBDATE", "updated"]
    list_display_links = ["entid", ]
    list_filter = ["CANREA", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)
    readonly_fields = ["updated", ]


class ELRevokeAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "REVDATE", "ISCAN", "updated"]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj",)
    readonly_fields = ["updated", ]


class ELRegNameAdmin(ELBaseAdmin):
    list_display = ["id", "ENTNAME", "NAMEDISTRICT", "ENTTRA", "updated"]
    search_fields = ["^ENTNAME", ]
    readonly_fields = ["updated", ]


class ELRegPreAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "ENTTYPE", "DOMDISTRICT", "CONGRO", "updated"]
    readonly_fields = ["updated", ]


class ELRegFormoneyAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "CONGRO", "REGCAP", "RECCAP", "DOMEREGCAP", "FORREGCAP", "updated"]
    readonly_fields = ["updated", ]


class ELFiliationAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "bentid", "BRNAME", "BRREGNO", "BRPRINCIPAL", "updated"]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj", "bentid",)
    readonly_fields = ["updated", ]


class ELInvAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "INV", "INVTYPE", "CONAM", "CONPROP", "CONFORM", "updated"]
    list_display_links = ["entid", ]
    search_fields = ("^entobj__ENTNAME",)
    raw_id_fields = ("entobj", "personid",)
    readonly_fields = ["updated", ]

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(ELInvAdmin, self).get_search_results(request, queryset, search_term)
        if search_term:
            zhpattern = re.compile(u'[\u4e00-\u9fa5]+')
            match = zhpattern.search(search_term)
            if match:
                from wlmodels.company.models import ELPost
                the_coms = ELPost.objects.filter(ENTNAME__istartswith=search_term).values_list("entid", flat=True)[:300]
                queryset = self.model.objects.filter(entobj__in=list(the_coms))
            else:
                queryset = self.model.objects.filter(CERNO__istartswith=search_term)
        return queryset, use_distinct


class ELStockImpawnAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "IMPORG", "IMPORGTYPE", "IMPAM", "updated"]
    list_display_links = ["entid", ]
    readonly_fields = ["updated", ]


class ELStockFrostAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "FROAM", "SHARFROPROP", "CORENTNAME", "updated"]
    list_display_links = ["entid", ]
    readonly_fields = ["updated", ]


class ELFinalShareHolderAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "FINALENTNAME", "FINALINVTYPE", "FINALSUBCONAM", "FINALCONFORM", "FINALCONDATE"]
    list_display_links = ["entid", ]


class ELPersonAdmin(ELBaseAdmin):
    list_display = ["personid", "NAME", "CERNO", "CERTYPE", "SEX"]
    list_display_links = ["NAME", ]
    search_fields = ("^CERNO",)
    readonly_fields = ["personid", "updated", ]


class PreReginvAdmin(ELBaseAdmin):
    list_display = ["NAME_ID", "INVID", "INV", "INVTYPE"]
    readonly_fields = ["updated", ]


class ELRegNameBanInfoAdmin(ELBaseAdmin):
    list_display = ["id", "BANLETTER", "BAN_ID", "BANLETTPINY", "BANTYPE"]
    list_display_links = ["BANLETTER", ]
    search_fields = ["^BANLETTER", ]
    readonly_fields = ["updated", ]


class ELRegNameDissensionAdmin(ELBaseAdmin):
    list_display = ["id", "DIS_ID", "APPEDATE", "APP1", "RES"]
    readonly_fields = ["updated", ]


class ELRegPreInvAdmin(ELBaseAdmin):
    list_display = ["id", "INV", "NAME_ID", "SUBCONAM", "CONPROP", ]
    readonly_fields = ["updated", ]


class ELWadenetAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "WEBSITNAME", "TYPOFWEB", "DOMAIN", "TYPEOFIDEN"]
    list_display_links = ["entid", ]
    readonly_fields = ["updated", ]


class ELStockContriFactAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "get_personid", "get_entid_inv", "CONTERMNO", "CURACTCONAM", "CONFORM", ]
    readonly_fields = ["updated", ]

    def get_personid(self, obj):
        if obj.personid:
            the_o = ELPerson.objects.filter(personid=obj.personid).first()
            if the_o:
                return the_o.NAME
        return ""

    get_personid.short_description = "关联个人"

    def get_entid_inv(self, obj):
        if obj.entid_inv:
            the_o = ELPost.objects.filter(entid=obj.entid_inv).first()
            if the_o:
                return the_o.ENTNAME
        return ""

    get_entid_inv.short_description = "关联企业"


class ELStockContriShouldAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "get_personid", "get_entid_inv", "CONTREMNO", "CURSUBCONAM", "CONFORM", ]
    readonly_fields = ["updated", ]

    def get_personid(self, obj):
        if obj.personid:
            the_o = ELPerson.objects.filter(personid=obj.personid).first()
            if the_o:
                return the_o.NAME
        return ""

    get_personid.short_description = "关联个人"

    def get_entid_inv(self, obj):
        if obj.entid_inv:
            the_o = ELPost.objects.filter(entid=obj.entid_inv).first()
            if the_o:
                return the_o.ENTNAME
        return ""

    get_entid_inv.short_description = "关联企业"


class ELStockTransferAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "ALIEN", "get_entid_inv", "get_personid_inv", "get_entid_trans", "get_personid_trans", "TRANAM"]
    readonly_fields = ["updated", ]

    def get_entid_inv(self, obj):
        if obj.entid_inv:
            the_o = ELPost.objects.filter(entid=obj.entid_inv).first()
            if the_o:
                return the_o.ENTNAME
        return ""

    get_entid_inv.short_description = "出让企业"

    def get_personid_inv(self, obj):
        if obj.personid_inv:
            the_o = ELPerson.objects.filter(personid=obj.personid_inv).first()
            if the_o:
                return the_o.NAME
        return ""

    get_personid_inv.short_description = "出让个人"

    def get_entid_trans(self, obj):
        if obj.entid_trans:
            the_o = ELPost.objects.filter(entid=obj.entid_trans).first()
            if the_o:
                return the_o.ENTNAME
        return ""

    get_entid_trans.short_description = "受让企业"

    def get_personid_trans(self, obj):
        if obj.personid_trans:
            the_o = ELPerson.objects.filter(personid=obj.personid_trans).first()
            if the_o:
                return the_o.NAME
        return ""

    get_personid_trans.short_description = "受让个人"


class ELGroupCancelAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "CANDOCNO", "CANREA", "CANDATE"]
    readonly_fields = ["updated", ]


class ELGroupAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "GRPNAME", "GRPSHFORM", "PCOMNAME", "ESDATE"]
    list_display_links = ["GRPNAME", ]
    readonly_fields = ["updated", ]


class ELGroupMemberAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "MEMNAME", "REGNO", "GRPMEMTYPE", "REGCAP"]
    list_display_links = ["MEMNAME", ]
    readonly_fields = ["updated", ]


class ELCaseSrcAdmin(ELBaseAdmin):
    list_display = ["id", "caseid", "CASESOU", "CLUECON"]
    search_fields = ("caseid",)
    readonly_fields = ["updated", ]


class ELCaseIllegalAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "EXETO", "JUDAUTH"]
    readonly_fields = ["updated", ]


class ELCaseWantedAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "WANT_ID", "WANTAUTH", ]
    readonly_fields = ["updated", ]


class ELLicenceAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "LICNAME", "LICSTATE", "VALFORM", "VALTO"]
    readonly_fields = ["updated", ]


class ELGetiLicenceAdmin(ELBaseAdmin):
    list_display = ["id", "entid", "LICNAME", "LICSTATE", "VALFORM", "VALTO"]
    readonly_fields = ["updated", ]
    search_fields = ["entid_id", ]

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(ELGetiLicenceAdmin, self).get_search_results(request, queryset, search_term)
        if search_term:
            from wlmodels.company.models import ELGeti
            the_coms = ELGeti.objects.filter(ENTNAME__istartswith=search_term).values_list("entid", flat=True)[:300]
            queryset = self.model.objects.filter(entid_id__in=list(the_coms))
        return queryset, use_distinct


class ELMoveInAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "MOUTAREA", "MINREA", "MINDATE", ]
    readonly_fields = ["updated", ]


class ELMoveOutAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "MINAREA", "MOUTREA", "MOUTDATE", ]
    readonly_fields = ["updated", ]


class ELBrandAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "TMNAME", "COGDATE", "TMTYPE", "APPPROD", ]
    readonly_fields = ["updated", ]


class ELAnnualCheckAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "ANCHEYEAR", "ANCHETYPE", "ANCHEFORM", "ANCHEDATE"]
    readonly_fields = ["updated", ]


class ELAnnualCheckInAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "ANCHEYEAR", "BUSST", "VENDINC", "PROGRO", "ASSGRO", "LIAGRO"]
    readonly_fields = ["updated", ]


class ELAnnualCheckForAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "ANCHEYEAR", "BUSST", "VENDINC", "PROGRO", "ASSGRO", "LIAGRO"]
    readonly_fields = ["updated", ]


class ELMortRegAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "MORREG_ID", "MORTYPE", "MORTGAGOR", "REGIDATE"]
    readonly_fields = ["updated", ]


class ELMortClaimAdmin(ELBaseAdmin):
    list_display = ["id", "MORREG_ID", "PRICLASECKIND", "PRICLASECAM", "WARCOV"]
    raw_id_fields = ["MORREG_ID", ]
    search_fields = ["MORREG_ID__entid__ENTNAME", ]
    readonly_fields = ["updated", ]


class ELMortGuaAdmin(ELBaseAdmin):
    list_display = ["id", "MORREG_ID", "GUANAME", "OWN", "GUADES"]
    raw_id_fields = ["MORREG_ID", ]
    search_fields = ["MORREG_ID__entid__ENTNAME", ]
    readonly_fields = ["updated", ]


class ELMortPersonAdmin(ELBaseAdmin):
    list_display = ["id", "MORREG_ID", "MORE", "MOR_LOC", ]
    raw_id_fields = ["MORREG_ID", ]
    search_fields = ["MORREG_ID__entid__ENTNAME", ]
    readonly_fields = ["updated", ]


class ELMortAlterAdmin(ELBaseAdmin):
    list_display = ["id", "MORREG_ID", "ALTCONITEM", "ALTDATE", "ALT"]
    raw_id_fields = ["MORREG_ID", ]
    search_fields = ["MORREG_ID__entid__ENTNAME", ]
    readonly_fields = ["updated", ]


class ELCreditAdmin(EntIdableAdmin):
    list_display = ["id", "entid", "index_total", "index_2_1", "index_2_2", "index_2_3", "index_2_4", "index_2_5", ]


admin.site.register(ELPost, ELPostAdmin)
admin.site.register(ELBasic, ELBasicAdmin)
# admin.site.register(ELClass, ELClassAdmin)
# admin.site.register(ELShort, ELShortAdmin)
# admin.site.register(ELNameHistory, ELNameHistoryAdmin)
# admin.site.register(ELTags, ELTagsAdmin)
# admin.site.register(ELTagsAssigned, ELTagsAssignedAdmin)
admin.site.register(ELManager, ELManagerAdmin)
admin.site.register(ELModify, ELModifyAdmin)
# admin.site.register(ELGeti, ELGetiAdmin)
admin.site.register(ELLiqentidation, ELLiqentidationAdmin)
admin.site.register(ELCaseBasic, ELCaseBasicAdmin)
admin.site.register(ELCaseDetail, ELCaseDetailAdmin)
admin.site.register(ELCasePunish, ELCasePunishAdmin)
admin.site.register(ELCancel, ELCancelAdmin)
admin.site.register(ELRevoke, ELRevokeAdmin)
# admin.site.register(ELRegName, ELRegNameAdmin)
# admin.site.register(ELRegPre, ELRegPreAdmin)
# admin.site.register(ELRegFormoney, ELRegFormoneyAdmin)
admin.site.register(ELFiliation, ELFiliationAdmin)
admin.site.register(ELInv, ELInvAdmin)
# admin.site.register(ELStockImpawn, ELStockImpawnAdmin)
# admin.site.register(ELStockFrost, ELStockFrostAdmin)
# admin.site.register(ELStockContriFact, ELStockContriFactAdmin)
# admin.site.register(ELStockContriShould, ELStockContriShouldAdmin)
# admin.site.register(ELStockTransfer, ELStockTransferAdmin)
# admin.site.register(ELFinalShareHolder, ELFinalShareHolderAdmin)
admin.site.register(ELPerson, ELPersonAdmin)
# admin.site.register(ELRegNameDissension, ELRegNameDissensionAdmin)
# admin.site.register(ELRegNameBanInfo, ELRegNameBanInfoAdmin)
# admin.site.register(ELRegPreInv, ELRegPreInvAdmin)
# admin.site.register(ELWadenet, ELWadenetAdmin)
# admin.site.register(ELGroup, ELGroupAdmin)
# admin.site.register(ELGroupCancel, ELGroupCancelAdmin)
# admin.site.register(ELGroupMember, ELGroupMemberAdmin)
# admin.site.register(ELCaseIllegal, ELCaseIllegalAdmin)
# admin.site.register(ELCaseSrc, ELCaseSrcAdmin)
# admin.site.register(ELCaseWanted, ELCaseWantedAdmin)
# admin.site.register(ELLicence, ELLicenceAdmin)
# admin.site.register(ELGetiLicence, ELGetiLicenceAdmin)
# admin.site.register(ELMoveIn, ELMoveInAdmin)
# admin.site.register(ELMoveOut, ELMoveOutAdmin)
# admin.site.register(ELBrand, ELBrandAdmin)
# admin.site.register(ELAnnualCheck, ELAnnualCheckAdmin)
# admin.site.register(ELAnnualCheckIn, ELAnnualCheckInAdmin)
# admin.site.register(ELAnnualCheckFor, ELAnnualCheckForAdmin)
# admin.site.register(ELMortReg, ELMortRegAdmin)
# admin.site.register(ELMortClaim, ELMortClaimAdmin)
# admin.site.register(ELMortPerson, ELMortPersonAdmin)
# admin.site.register(ELMortAlter, ELMortAlterAdmin)
# admin.site.register(ELMortGua, ELMortGuaAdmin)
# admin.site.register(ELCredit, ELCreditAdmin)
